/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager.create;

import br.com.foxline.atlasmobile.enums.Patrimonio;
import br.com.foxline.model.Coordenada;
import br.com.foxline.model.Imovel;
import br.com.foxline.model.Proprietario;
import br.com.foxline.service.DneService;
import br.com.foxline.service.ImovelService;
import br.com.foxline.service.ProprietarioService;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;
import org.primefaces.model.map.*;

/**
 *
 * @author roniere
 */
@ViewScoped
@ManagedBean
public class ManagerMapa implements Serializable {

    @EJB
    ImovelService imovelDao;
    @EJB
    ProprietarioService proprietarioService;
    @EJB
    DneService dneService;
    private MapModel mapModel;
    private List<Imovel> imoveis;
    private Marker marker;
    private Imovel imovel;
    private String centro;
    private Double latitude = 0D;
    private Double longitude = 0D;
    private PieChartModel pieModel;
    private PieChartModel pieModelPendente;
    private CartesianChartModel categoryModel;
    private Long max;

    @PostConstruct
    private void init() {
        imovel = new Imovel();
        centro = "-2.9136, -41.7577";
        mapModel = new DefaultMapModel();
        max = 10L;
        createPieModel();
        createCategoryModel();
    }

    public Long getMax() {
        return max;
    }

    public void setMax(Long max) {
        this.max = max;
    }

    public void plotar() {
        latitude = 0D;
        longitude = 0D;

        LatLng coord = null;
        Polygon polygon = new Polygon();
        for (Imovel i : imoveis) {

            i.setProprietarios(proprietarioService.getProprietarioImovel(i.getId()));

            for (Coordenada c : i.getCoordenadas()) {
                coord = new LatLng(c.getLat(), c.getLog());

                final Marker marker = new Marker(coord, "Proprietário: ");
                marker.setData(i);

                if (i.getProprietarios() == null || i.getProprietarios().isEmpty()) {
                    marker.setTitle("Não existe(m) proprietário(s)");
                }
                for (Proprietario p : i.getProprietarios()) {
                    marker.setTitle(marker.getTitle() + ", " + p.getNome());
                }
                if (i.getCoordenadas() != null && !i.getCoordenadas().isEmpty()) {
                    try {
                        latitude += (latitude + c.getLat()) / i.getCoordenadas().size();
                        longitude += (longitude + c.getLog()) / i.getCoordenadas().size();
                    } catch (Exception e) {
                    }
                }
                mapModel.addOverlay(marker);
            }
        }
        centro = latitude + ", " + longitude;

    }

    public void search() {
        imoveis = imovelDao.findImoveis(imovel, null, null);
        mapModel = new DefaultMapModel();
        plotar();
    }

    public void onMarkerSelect(OverlaySelectEvent event) {
        marker = (Marker) event.getOverlay();
    }

    public Marker getMarker() {
        return marker;
    }

    public void setMarker(Marker marker) {
        this.marker = marker;
    }

    public MapModel getMapModel() {
        return mapModel;
    }

    public void setMapModel(MapModel mapModel) {
        this.mapModel = mapModel;
    }

    public List<Imovel> getImoveis() {
        return imoveis;
    }

    public void setImoveis(List<Imovel> imoveis) {
        this.imoveis = imoveis;
    }

    public Imovel getImovel() {
        return imovel;
    }

    public void setImovel(Imovel imovel) {
        this.imovel = imovel;
    }

    public String getCentro() {
        return centro;
    }

    public void setCentro(String centro) {
        this.centro = centro;
    }

    private void createPieModel() {
        pieModel = new PieChartModel();

        for (String str : dneService.getAllBairro()) {
            pieModel.set(str, imovelDao.diagQtdImovelPorBairro(str));
        }

        pieModelPendente = new PieChartModel();
        Long total = qtdImovel();
        Long totalPendente = qtdImovelPendente();
        pieModelPendente.set("Imóveis Finalizados", total - totalPendente);
        pieModelPendente.set("Imóveis Pendentes", totalPendente);

    }

    public Long qtdImovel() {
        return imovelDao.diagQtdImovel();
    }

    public Long qtdImovelPendente() {
        return imovelDao.diagQtdImovelPendente();
    }

    public PieChartModel getPieModel() {
        return pieModel;
    }

    public void setPieModel(PieChartModel pieModel) {
        this.pieModel = pieModel;
    }

    public PieChartModel getPieModelPendente() {
        return pieModelPendente;
    }

    public void setPieModelPendente(PieChartModel pieModelPendente) {
        this.pieModelPendente = pieModelPendente;
    }

    private void createCategoryModel() {
        categoryModel = new CartesianChartModel();

        for (Patrimonio p : Patrimonio.values()) {
            Imovel imovelFind = new Imovel();
            ChartSeries boys = new ChartSeries();
            boys.setLabel(p.getNome());
            imovelFind.setPatrimonio(p);
            Long resu = imovelDao.diagFindImoveis(imovelFind, null, null);
            
            if(resu>max){
                max = resu;
            }
            
            boys.set("", resu);
            categoryModel.addSeries(boys);
        }
        
        max += 10;
    }

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    public void setCategoryModel(CartesianChartModel categoryModel) {
        this.categoryModel = categoryModel;
    }
}
